package madixin.t3;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int res = 0;
        int left = -1;
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i)) && map.get(s.charAt(i)) > left) {
                left = map.get(s.charAt(i));
            }
            map.put(s.charAt(i), i);
            res = Math.max(res, i - left);
        }
        return res;
    }


    public int lengthOfLongestSubstring2(String s) {
        int res = 0;
        int left = 0;
        int right = 0;
        Set<Character> set = new HashSet<>();
        while (right < s.length()) {
            if (!set.contains(s.charAt(right))) {
                set.add(s.charAt(right));
                right++;
                res = Math.max(res, right - left);
            } else {
                while (left <= right) {
                    set.remove(s.charAt(left));
                    if (s.charAt(left++) == s.charAt(right)) {

                        break;
                    }
                }
            }
        }
        return res;
    }


    public static void main(String[] args) {
        Solution s = new Solution();
//        System.out.println(s.lengthOfLongestSubstring("abcabcbb"));
//        System.out.println(s.lengthOfLongestSubstring("bbbbb"));
//        System.out.println(s.lengthOfLongestSubstring("pwwkew"));
//        System.out.println(s.lengthOfLongestSubstring("tmmzuxt"));
//        System.out.println(s.lengthOfLongestSubstring("dvdf"));
        System.out.println(s.lengthOfLongestSubstring(" "));


    }
}